package com.saul.server.business.pc28.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.saul.server.business.pc28.entity.*;
import com.saul.server.business.pc28.mapper.ActualResultMapper;
import com.saul.server.business.pc28.mapper.Canada28100Mapper;
import com.saul.server.business.pc28.mapper.Canada2880Mapper;
import com.saul.server.business.pc28.mapper.Canada28IntervalMapper;
import com.saul.server.business.pc28.service.ActualResultService;
import com.saul.server.business.pc28.service.Canada28DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @author 枫伦
 * @DESCRIPTION
 * @create 2021/6/13 4:24 下午
 */
@Component
@Slf4j
public class SyncResultTask {
    @Autowired
    Canada28DataService canada28DataService;
    @Autowired
    ActualResultService actualResultService;
    @Autowired
    ActualResultMapper actualResultMapper;
    @Autowired
    Canada2880Mapper canada2880Mapper;
    @Autowired
    Canada28100Mapper canada28100Mapper;
    @Autowired
    Canada28IntervalMapper intervalMapper;

    @Scheduled(cron = "30 0/4 * * * ?")
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void addForcastData() {
//        log.info("期数数据同步开始" + LocalDateTime.now());
        ActualResult actuakDataNewest = null;
        try {
            //从网上获取最新的一期
            actuakDataNewest = canada28DataService.getActuakDataNewest();
            if (ObjectUtil.isNull(actuakDataNewest)) {
                for (int i = 0; i < 6; i++) {
                    Thread.sleep(12000);
                    log.info("网上获取最新一期结果失败,重试次数: " + (1 + i));
                    actuakDataNewest = canada28DataService.getActuakDataNewest();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        if (ObjectUtil.isNull(actuakDataNewest)) {
            log.info("网上获取最新一期结果失败,结束此次更新");
            return;
        }
        log.info(JSON.toJSONString(actuakDataNewest));
        //从数据库中更据时间查询出最新的一期实际结果

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("issue_number", actuakDataNewest.getIssueNumber());
        queryWrapper.eq("is_open", 0);
        ActualResult one = actualResultService.getOne(queryWrapper);

        QueryWrapper issueNumberWrapper = new QueryWrapper();
        issueNumberWrapper.eq("issue_number", actuakDataNewest.getIssueNumber());
        if (ObjectUtil.isNull(one)) {
            //没有这一期的信息直接插入数据库
            actualResultMapper.insert(actuakDataNewest.setIsOpen(0));

            //更新预测表中的数据1.获取预测的数据 2.修改每一条预测的数据
            //Canada2880的更新
            List<Canada2880> canada2880List = canada2880Mapper.selectList(issueNumberWrapper);
            if (CollectionUtil.isNotEmpty(canada2880List)) {
                for (Canada2880 canada2880 : canada2880List) {
                    getResultByCompare(actuakDataNewest, canada2880);
                    canada2880Mapper.updateById(canada2880);
                }
            }
            //Canada28100的更新
            List<Canada28100> canada28100List = canada28100Mapper.selectList(issueNumberWrapper);
            if (CollectionUtil.isNotEmpty(canada28100List)) {
                for (Canada28100 canada28100 : canada28100List) {
                    getResultByCompare(actuakDataNewest, canada28100);
                    canada28100Mapper.updateById(canada28100);
                }
            }
        }

        intervalRemind(actuakDataNewest);
    }

    /*间隔期数提示
     * 大小间隔
     * 单双间隔
     * 对错间隔
     * */
    private void intervalRemind(ActualResult actuakDataNewest) {
        List<Canada28Interval> canada2880List = intervalMapper.selectByAlgorithmType();
        if (CollectionUtil.isEmpty(canada2880List)) {
            //说明没有数据//插入几条数据
            Canada28Interval interval = new Canada28Interval();
            interval.setId(1L);
            interval.setAlgorithmType(1);
            setIntervalValue(actuakDataNewest, interval);
            intervalMapper.insert(interval);

            Canada28Interval interval2 = new Canada28Interval();
            interval2.setId(2L);
            interval2.setAlgorithmType(2);
            setIntervalValue(actuakDataNewest, interval2);
            intervalMapper.insert(interval2);

            Canada28Interval interval3 = new Canada28Interval();
            interval3.setId(3L);
            interval3.setAlgorithmType(3);
            setIntervalValue(actuakDataNewest, interval3);

            intervalMapper.insert(interval3);
        }
        for (Canada28Interval interval : canada2880List) {
            setIntervalValue(actuakDataNewest, interval);
            if (interval.getBig() >= 5 || interval.getSmall() >= 5 || interval.getSingle() >= 5 || interval.getDoubles() >= 5) {
                log.info("当前期数:" + actuakDataNewest.getIssueNumber() + "---" + JSON.toJSONString(interval));
            }
            //对错-记录第一次的
            intervalMapper.updateById(interval);
        }


    }

    private void setIntervalValue(ActualResult actuakDataNewest, Canada28Interval interval) {
        //大小
        if (actuakDataNewest.getActualSum() >= 14) {
            interval.setBig(interval.getBig() + 1);
            interval.setSmall(0);
        } else {
            interval.setBig(0);
            interval.setSmall(interval.getSmall() + 1);
        }
        //单双
        if (actuakDataNewest.getActualSum() % 2 == 0) {
            interval.setDoubles(interval.getDoubles() + 1);
            interval.setSingle(0);
        } else {
            interval.setDoubles(0);
            interval.setSingle(interval.getSingle() + 1);
        }
    }

    private void getResultByCompare(ActualResult actuakDataNewest, Canada28 canada28) {
        canada28.setActualNum1(actuakDataNewest.getActualNum1())
                .setActualNum2(actuakDataNewest.getActualNum2())
                .setActualNum3(actuakDataNewest.getActualNum3())
                .setActualSum(actuakDataNewest.getActualSum())
                .setActualSize(actuakDataNewest.getActualSize())
                .setActualSingleDouble(actuakDataNewest.getActualSingleDouble())
                .setResultSize(StringUtils.isEmpty(canada28.getForecastSize()) ? "错" : canada28.getForecastSize().equals(actuakDataNewest.getActualSize()) ? "对" : "错")
                .setResultSingleDouble(StringUtils.isEmpty(canada28.getForecastSingleDouble()) ? "错" : canada28.getForecastSingleDouble().equals(actuakDataNewest.getActualSingleDouble()) ? "对" : "错");
    }
}
